package impl;

import gui.ChatGUI;

import interfaces.ChatProxy;
import interfaces.ChatServer;
import interfaces.ClientProxy;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Methodimplementation of clientproxy.
 * Implements methods for server to communicate with clients.
 *
 * @author Dominik Korittki
 * @author Steffen Grushka
 * @version 0.1
 */
public class ClientProxyImpl extends UnicastRemoteObject implements ClientProxy {

	private static final long serialVersionUID = 1L;
	private ChatGUI gui;
	private ChatProxy chat;
	private ChatServer chatServer;
	private String username;
	private String ip;
	
	protected ClientProxyImpl() throws RemoteException {
		super();
		gui = new ChatGUI(this);
		gui.setVisible(true);
		
		//start pop-up window, request for ip and username
		ip = gui.ipRequest();
		if(ip == null || ip.equals("")) {
			gui.appendMessage("no valid ip address given. No connection attempt.");
		} else {
			username = gui.usernameRequest();
			System.out.println("Username is " + username);
			if(username == null || username.equals("")) {
				gui.appendMessage("No username entered. No connection attempt.");
			} else {
				
				//connect to server
				gui.appendMessage("Connect to Server ...");
				try {
					Registry registry = LocateRegistry.getRegistry(ip);
					chatServer = (ChatServer) registry.lookup("ChatServer");
					
					chat = chatServer.subscribeUser(username, this);
					
					if(chat == null) {
						System.out.println("chat = null, problem with chat object.");
						gui.appendMessage("Server refused connection attempt.");
					} else {
						gui.appendMessage("Connection established.");
					}
				} catch (Exception e) {
					gui.appendMessage("Server inaccessible.");
					System.out.println("Exception was thrown. server running and no networking problems?");
				} 
			}
		}
	}
	
	@Override
	public void receiveMessage(String username, String message) throws RemoteException {
		SimpleDateFormat sdf = new SimpleDateFormat();
	    sdf.applyPattern( "hh:mm:ss" );
	    
		gui.appendMessage(username + "(" + sdf.format(new Date()) + "): " + message);
	}
	
	/**
	 * called by gui actionlistener to send a message to the chatserver
	 * @param message
	 */
	public void sendMessage(String message) {
		try {
			chat.sendMessage(message);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * called by gui listener if clients close the window.
	 * disconnects from server
	 */
	public void logout() {
		try {
			if(chatServer != null)
				chatServer.unsubscribeUser(username);
		} catch (RemoteException e) {}
	}

	/**
	 * creates new clientproxyimpl(this class) object
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			new ClientProxyImpl();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

}
